package com.liusr.template.controller;

import com.liusr.template.common.result.Result;
import com.liusr.template.common.result.ResultData;
import com.liusr.template.model.User;
import com.liusr.template.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.liusr.template.constant.StringConstants.*;

/**
 * @author Liusr
 * @date 2023/1/23
 * @package com.liusr.template.controller
 * @Description: 用户controller类
 */
@RestController
@Api(tags = "用户控制器")
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@PostMapping("/addUser")
	@ApiOperation(value = "添加用户",notes = "添加用户信息")
	public Result<User> addUser(@RequestBody User user) {
		boolean addUser = userService.addUser(user);
		return addUser ? ResultData.success(RETURN_OPERATION_SUCCESS) : ResultData.fail(RETURN_OPERATION_FAILED);
	}

	@DeleteMapping("/deleteUser")
	public Result<User> deleteUserById(@RequestParam("id") Long id) {
		if (id == null || id == 0) {
			return ResultData.fail(EXCEPTION_PARAM_ERROR);
		}
		boolean delete = userService.deleteUserById(id);
		return delete ? ResultData.success(RETURN_OPERATION_SUCCESS) : ResultData.fail(RETURN_OPERATION_FAILED);
	}

	@PutMapping("/isDeleteUser")
	public Result<User> IsDeleteUserById(@RequestParam("id") Long id) {
		if (id == null || id == 0) {
			return ResultData.fail(EXCEPTION_PARAM_ERROR);
		}
		boolean isDelete = userService.isDeleteById(id);
		return isDelete ? ResultData.success(RETURN_OPERATION_SUCCESS) : ResultData.fail(RETURN_OPERATION_FAILED);
	}

	@PutMapping("/updateUser")
	public Result<User> updateUserById(@RequestBody User user) {
		boolean update = userService.updateUserById(user);
		return update ? ResultData.success(RETURN_OPERATION_SUCCESS) : ResultData.fail(RETURN_OPERATION_FAILED);
	}

	@GetMapping("/selectUserById")
	public Result<User> selectUserById(@RequestParam("id") Long id) {
		User user = userService.selectUserById(id);
		return ResultData.ok(RETURN_OPERATION_SUCCESS, user);
	}

	@GetMapping("/selectUserLIke")
	public Result<Object> selectUserLike(@RequestParam("username") String username, @RequestParam("age") Long age, @RequestParam("studentMajor") String studentMajor) {
		List<User> userList = userService.selectUserLike(username, age, studentMajor);
		return ResultData.ok(RETURN_OPERATION_SUCCESS, userList);
	}

	@GetMapping("/selectAllUser")
	public Result<Object> selectAllUser() {
		List<User> userList = userService.selectAllUser();
		return ResultData.ok(RETURN_OPERATION_SUCCESS, userList);
	}
}
